﻿using Sunny.UI;
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
using CoreShop.Model.FromBody;
using CoreShop.Library;
using System.Linq;
using CoreShop.App.Units;
using CoreShop.Model;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
using CoreShop.Services.Services.Report;
using CoreShop.Services;

namespace CoreShop.App
{
    public partial class fpgPaymentDetail : UIPage
    {
        PosReportRequest mRptRequest;
        public int mPageIndex = 0;
        public fpgPaymentDetail()
        {
            InitializeComponent();
            LoadComboxData();
        }

        private void LoadComboxData()
        {
            #region 加载支付方式
            this.tab3_payment.Items.Clear();
            var payList = new corecmspaymentsService().AsQueryable().Where(p => p.isEnable == true).ToList();
            tab3_payment.DataSource = payList;
            tab3_payment.DisplayMember = "name";
            tab3_payment.ValueMember = "id";
            tab3_payment.Text = "";
            tab3_payment.SelectedValue = 0;
            #endregion
        }

        private void fpgPaymentDetail_ReceiveParams(object sender, UIPageParamsArgs e)
        {
            mPageIndex = 1;
            mRptRequest = e.Value as PosReportRequest;
            if (mRptRequest.type == (int)GlobalEnumVars.ReportRequest.Query)
            {
                LoadData(mRptRequest);
            }
            else
            {
                ExportData();
            }
        }

        private void ExportData()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Export Excel File To";
            saveFileDialog.ShowDialog();
            string fileName = saveFileDialog.FileName;
            if (fileName == "") return;
            Dictionary<string, DataGridView> gridViews = new Dictionary<string, DataGridView>();
            gridViews.Add("支付明细", dgvPayList);
            if (ExportUtil.NPOIExportForDataGridview(gridViews, fileName))
            {
                MessageBox.Show("导出Excel成功！");
            }
        }


        /// <summary>
        ///销售统计
        /// </summary>
        private void LoadData(PosReportRequest posReport)
        {
            int count = 0;
            List<Reportscorecmsfoodorderpayment> lstSource = new corereportServices().GetOrderPaymentDetail(ref count, mPageIndex, btnPg4.PageSize, posReport.starTime, posReport.endTime, 0, CommFunc.ConvertToInt32(tab3_payment.SelectedValue));

            btnPg4.TotalCount = count;
            this.dgvPayList.AutoGenerateColumns = false;
            this.dgvPayList.DataSource = lstSource;
            //uiDataGridViewFooter1.DataGridView = sumRow;
            //设置统计绑定的表格
            PageTotal.DataGridView = dgvPayList;
            PageTotal["column4_orderNo"] = "合计：";
            PageTotal["payAmount"] = CommFunc.ConvertToString(lstSource.Sum(p=>p.payAmount));
            PageTotal["count"] = CommFunc.ConvertToString(lstSource.Sum(p => p.count));
            PageTotal["refundCount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.refundCount));
            PageTotal["refundAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.refundAmount));
        }

        private void btnPg4_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            mPageIndex = pageIndex;
            LoadData(mRptRequest);
        }
    }
}